<template>
  <Layout>
    <div class="navBar">
      <i @click="goBack" class="iconfont icon-rightarrow-left"></i>
      <span class="title">编辑标签</span>
      <i class="iconfont"></i>
    </div>
    <FormItem class="form-item" field-name="标签名"
              @update:value="update"
              :value="tag.name" placeholder="请输入标签名"/>
    <div class="button-wrapper">
      <Button @click="remove">删除标签</Button>
    </div>
  </Layout>
</template>

<script lang="ts">
import Vue from 'vue';
import {Component} from 'vue-property-decorator';
import FormItem from '@/components/money/FormItem.vue';
import Button from '@/components/Button.vue';

@Component({
  components: {Button, FormItem}
})
export default class EditLabel extends Vue {
  tag?: Tag = undefined;


  created() {
    this.tag = this.$store.state.tagList.find((item:Tag) => item.id === this.$route.params.id);
    if (!this.tag) {
      this.$router.replace('/404');
     }
  }

  update(name: string) {
    if (this.tag) {
      this.$store.commit('updateTag', {id:this.tag.id, name});

    }
  }

  remove() {
    if (this.tag) {
      this.$store.commit('removeTag',this.tag.id)
    }

  }

  goBack() {
    this.$router.back();
  }
}
</script>

<style lang="scss" scoped>
.navBar {
  background-color: #fff;
  padding: 5px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;

  > i {
    padding: 10px;
  }

  > .title {
  }
}

.form-item {
  margin-top: 20px;
}

.button-wrapper {
  text-align: center;
  margin-top: 44-16px;
}
</style>
